<?php


namespace app\system\controller;

class Admin extends AdminBase
{
	public function getlist()
	{
		$options = $this->request->get();
		$options["limit"] = $this->page_size;
		$options["page"] = $this->page_no;
		$result = \app\system\service\AdminService::getlist($options);
		$this->response($result["code"], "获取成功", $result["data"], ["count", $result["count"]], ["pages", $result["pages"]]);
	}
	public function add()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["account" => "require|length:1,32", "password" => "require|length:6,32", "name" => "require|length:1,16", "role_id" => "require", "disable" => "require|in:0,1"], ["account.require" => "账号不能为空", "account.length" => "账号名的长度为1到32位之间", "password.require" => "密码不能为空", "password.length" => "密码长度必须为6到16位之间", "name.require" => "名称不能为空", "name.length" => "账号名的长度为1到32位之间", "role_id.require" => "请选择角色", "disable.require" => "状态错误", "disable.in" => "状态错误"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$result = \app\system\service\AdminService::add($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "添加成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function getinfo($id)
	{
		$options = ["id" => $id];
		$result = \app\system\service\AdminService::getinfo($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "获取成功", $result["data"]);
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function edit()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["id" => "require", "name" => "require|length:1,16", "role_id" => "require", "disable" => "require|in:0,1"], ["id.require" => "id不能为空", "name.require" => "名称不能为空", "name.length" => "账号名的长度为1到32位之间", "role_id.require" => "请选择角色", "disable.require" => "状态错误", "disable.in" => "状态错误"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$options["admin_info"] = $this->admin_info;
		$result = \app\system\service\AdminService::edit($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "修改成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function del()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["id" => "require"], ["id.require" => "id不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$options["admin_info"] = $this->admin_info;
		$result = \app\system\service\AdminService::del($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "删除成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
	public function revise_verify_login_ip()
	{
		$options = $this->request->post();
		$validate = \think\Validate::make(["id" => "require"], ["id.require" => "id不能为空"]);
		if (!$validate->check($options)) {
			$this->response(\app\system\model\Code::fail, $validate->getError());
		}
		$options["admin_info"] = $this->admin_info;
		$result = \app\system\service\AdminService::revise_verify_login_ip($options);
		if ($result["code"] == 1) {
			$this->response($result["code"], "修改成功");
		} else {
			$this->response($result["code"], $result["msg"]);
		}
	}
}